﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic;
using umbraco.BusinessLogic.console;
using umbraco;

namespace AutoFolders.Providers
{
    public class AlphaFolderProvider : BaseFolderProvider
    {
        private string m_folderDocTypeAlias = null; 

        private bool m_useUppercase = false;
        private bool m_useUppercaseChecked = false;

        public bool UseUppercase
        {
            get
            {
                if (!m_useUppercaseChecked)
                {
                    if (base.Properties.ContainsKey("UseUppercase"))
                    {
                        m_useUppercase = Convert.ToBoolean(base.Properties["UseUppercase"]);
                    }
                    m_useUppercaseChecked = true;
                }

                return m_useUppercase;
            }
        }

        public override int CreatedFolderDepth
        {
            get
            {
                return 1;
            }
        }

        private string GetFirstLetter(string nodeName)
        {
            char firstLetter = nodeName.ToCharArray()[0];

            if (UseUppercase)
                return firstLetter.ToString().ToUpper();
            else
                return firstLetter.ToString().ToLower();
        }

        #region IFolderProvider Members


        public override int FindValidFolderId(Document newDocument, int iNodeRootFolder)
        {
            int validFolderId = -1;
            string docFirstLetter = GetFirstLetter(newDocument.Text);

            if (newDocument.Level > 1)
            {
                CMSNode parentNode = new CMSNode(iNodeRootFolder);

                // Firstly, check if user is adding document to auto-created folder (e.g. 2009/09)
                if (parentNode.Level > 1)
                {
                    if (parentNode.Text == docFirstLetter)
                        return parentNode.Id;
                }

                // Secondly check for child folder that was previously auto-created

                foreach (IconI alphaIcon in parentNode.Children)
                {
                    if (alphaIcon.Text == docFirstLetter)
                    {
                        validFolderId = alphaIcon.Id;
                        break;
                    }
                }

            }

            return validFolderId;
        }

        public override int CreateFolderStructure(Document newDocument, int iNodeRootFolder, string folderDocTypeAlias)
        {
            string docFirstLetter = GetFirstLetter(newDocument.Text);
            
            // Check if YEAR folder has already been created.
            int rootDocumentId = -1;
            if (newDocument.Level > 1)
            {
                rootDocumentId = iNodeRootFolder;
            }

            Document alphaDocument = Document.MakeNew(docFirstLetter, DocumentType.GetByAlias(folderDocTypeAlias), newDocument.User, rootDocumentId);
            alphaDocument.Publish(newDocument.User);
            library.UpdateDocumentCache(alphaDocument.Id);

            return alphaDocument.Id;
        }
    }

        #endregion
}
